# -*- coding: utf-8 -*-
import maya.cmds as cmds
import maya.mel as mel
import sys
# reload(sys)
# sys.setdefaultencoding('utf-8')
class Check_Export_Cam_ABC():
    def Cam_Bake(self, Endings, Exp_Path):
        file_path = cmds.file(q=True, sceneName=True)
        file_name = file_path.split('/')[-1]
        file_name = file_name.split('.')
        CamShapeTmps = ['front', 'persp', 'side', 'top']
        AllCamShapes = cmds.listCameras()
        ListCam = list(set(AllCamShapes) ^ set(CamShapeTmps))
        if len(ListCam) == 1:
            Old_Cam = cmds.ls(ListCam)[0]
            New_CamName = Old_Cam + Endings
            New_Cam = cmds.rename(cmds.camera()[0], New_CamName)
            cmds.parentConstraint(Old_Cam, New_Cam, name=New_Cam + 'Constraint')
            New_Cam_shape = cmds.listRelatives(New_Cam, shapes=True, children=True)[0]
            Old_Cam_shape = cmds.listRelatives(Old_Cam, shapes=True, children=True)[0]
            cmds.connectAttr(Old_Cam_shape + ".focalLength", New_Cam_shape + ".focalLength")
            st = int(cmds.playbackOptions(min=1, q=1))
            et = int(cmds.playbackOptions(max=1, q=1))
            cmds.select(New_Cam, replace=True)
            cmds.bakeResults(New_Cam, t=(st, et), sb=1)

            # 获取选中物体上的所有约束节点
            constraints = cmds.listRelatives(New_Cam,type="constraint")
            # 删除约束节点
            if constraints:
                cmds.delete(constraints)

            Export_Path = Exp_Path.replace('\\', '/') + '/' + file_name[0] + '_cam.abc'
            Export_Path = Export_Path.replace('\\', '/')
            print(Export_Path)
            mel.eval('AbcExport -j "-frameRange ' + str(st) + ' ' + str(et) + ' -dataFormat ogawa -root |' + New_Cam + ' -file '+Export_Path+'";')

            cmds.delete(cmds.ls(New_Cam))
            return ("YES")
        else:
            return ("NO")